/**
 * 数据存储类
 * 单例模式
 */

import PlayData from "./PlayData";
import gameShareAdsVideo from "./gameShareAdsVideo";

 export default class storage{
    public static _instance:storage = null
    public static get Instance(){
        if (null == this._instance) {
            this._instance = new storage();
        }
        return this._instance
    }

    public eneryTime = 0  //记录倒计时
    public nowEnery:number = 0   //当前体力值
    public addgoldcishu:number = 5
    public isfirstlogin:boolean = true
    public jilvchenghao:number = -1
    public jilvherolv:number = -1

    setCheckPoint(t){
        cc.sys.localStorage.setItem("checkpoint", t)
    }
    getCheckPoint(){
        var t = cc.sys.localStorage.getItem("checkpoint");
        return t = t || 1 ,Number(t)
    }

    //找不同 
    setCheckPoint_diff(t){
        let info = JSON.stringify(t)
        cc.sys.localStorage.setItem('checkpoint_diff', info)
        //cc.sys.localStorage.setItem("checkpoint_diff", t)
    }
    getCheckPoint_diff(){
        let t = cc.sys.localStorage.getItem('checkpoint_diff')
        if(t === undefined || t === null || t === 'NaN' || t === ''){
            t = [[0],[0],[0],[0],[0]] //字，图，找茬 0开启，1通过 -1或者无为未通过
        }else{
            t = JSON.parse(t)
        }
        return t
        // var t = cc.sys.localStorage.getItem("checkpoint_diff");
        // return t = t || 1 ,Number(t)
    }

    //记录高分 或者关卡总是 用于排行
    setHightScore(t){
        cc.sys.localStorage.setItem("hightscore", t)
        gameShareAdsVideo.Instance.upWxHighScore(t, ()=>{})
    }
    getHightScore(){
        var t = cc.sys.localStorage.getItem("hightscore");
        return t = t || 0 ,Number(t)
    }

    setGold(t) {
        cc.sys.localStorage.setItem("gold", t)
    }
    getGold() {
        var t = cc.sys.localStorage.getItem("gold");
        if(t === undefined || t === null  || t === 'NaN' || t === ''){
            t = 0
            this.setGold(0)
        }
        return Number(t) 
    }
    addGold(t){ 
        let gold = this.getGold()
        gold += t
        this.setGold(gold)
    }

    setEnergy(t: number) {
        // 确保设置的体力值不超过10
        if (t > 10) {
            t = 10;
        }
        // 更新内存中的体力值
        this.nowEnery = t;
        // 持久化体力值到本地存储
        cc.sys.localStorage.setItem("tili", t.toString());
        // 可选：打印设置的体力，用于调试
        console.log('设置体力', t);
    }
    getEnergy(){
        var t = cc.sys.localStorage.getItem("tili");
        if(t === undefined || t === null || t === 'NaN' || t === ''){
            t = 10
            this.setEnergy(10)
        }
        this.nowEnery = Number(t) 
        //console.log('获取的数据', this.nowEnery)
        return this.nowEnery
    }

    setTiShi(t) {
        cc.sys.localStorage.setItem("tishi", t)
    }
    getTiShi(){
        var t = cc.sys.localStorage.getItem("tishi");
        if(t === undefined || t === null || t === 'NaN' || t === ''){
            t = 5
            this.setTiShi(5)
        }
        return Number(t) 
    }
    
    /**
     * 玩家信息
     */
    setUserInfo(){

        console.log('存储userinfo')
        let info = JSON.stringify(PlayData.Instance.userInfo)
        //console.log('打印加密数据', info) 
        cc.sys.localStorage.setItem('userinfo', info)
    }
    getUserInfo(){
        let _info = cc.sys.localStorage.getItem('userinfo') || null
        if(_info) {
            let info = JSON.parse(_info)
            //console.log('打印解密数据', info) 
            PlayData.Instance.userInfo.hightScore = info.hightScore
            PlayData.Instance.userInfo.icon = info.icon
            PlayData.Instance.userInfo.openid = info.openid
            PlayData.Instance.userInfo.name = info.name
        }
        return PlayData.Instance.userInfo
    }

}
